﻿package 
{
	//WEEK 04
	import flash.display.MovieClip;
	import flash.display.FrameLabel;
	import flash.events.*;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.geom.Point;
	
	
	public class GameDoc extends MovieClip{
		//w2 Menu Constants
		static public const MENU:String = "menu";
		static public const GAME:String = "game";
		static public const END:String = "results";
		
		//w3 Keys
		static public const LEFT:int = Keyboard.LEFT;
		static public const RIGHT:int = Keyboard.RIGHT;
		static public const DOWN:int = Keyboard.DOWN;
		static public const UP:int = Keyboard.UP;
				
		//w3 Movement Properties
		protected var _power:int = 3;
		protected var _leftPressed:Boolean = false;
		protected var _rightPressed:Boolean = false;
		protected var _upPressed:Boolean = false;
		protected var _downPressed:Boolean = false;

		protected var _playerNextX:int;
		protected var _playerNextY:int;

		protected var _blhit:Boolean;
		protected var _brhit:Boolean;
		protected var _tlhit:Boolean;
		protected var _trhit:Boolean;
		
		public function GameDoc(){
			//setup frames of main movie to send an event when they are reached
			enumerateFrameLabels();
			addEventListener(MENU, setupMenu,false,0,true);
			addEventListener(GAME, setupGame,false,0,true);
			addEventListener(END, setupEnd,false,0,true);
		}
		
		private function dispatchFrameEvent():void{
			stop();
			dispatchEvent(new Event(currentLabel));
		}
		private function enumerateFrameLabels():void{
			for each (var label:FrameLabel in currentLabels)
				addFrameScript(label.frame-1, dispatchFrameEvent);
		}
		protected function setupMenu(e:Event){
			trace("setupMenu");
			test_button.button_text.text = "Start";
			test_button.addEventListener(MouseEvent.CLICK, aClick,false,0,true);
		}
		
		protected function setupGame(e:Event){
			end_button.button_text.text = "Win";
			end_button.addEventListener(MouseEvent.CLICK, bClick,false,0,true);
			// add keyboard listeners
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKDown,false,0,true);
			stage.addEventListener(KeyboardEvent.KEY_UP,onKUp,false,0,true);
			// add the main game loop listener
			addEventListener(Event.ENTER_FRAME,update,false,0,true);
			
			
		}
		protected function setupEnd(e:Event){
			
			start_button.button_text.text = "Menu";
			start_button.addEventListener(MouseEvent.CLICK, cClick,false,0,true);
			
		}
		
		protected function cleanUpGame()
		{
			//Remove Keyboard listeners and Enter frame listener
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP,onKDown);
			removeEventListener(Event.ENTER_FRAME,update);
		}
		
		protected function exitGame()
		{
			cleanUpGame();
			gotoAndPlay(END);
		}
		
		
		//START Button Management Code		
		protected function aClick(e:Event){
			gotoAndPlay(GAME);
		}
		protected function bClick(e:Event){
			gotoAndPlay(END);
		}
		protected function cClick(e:Event){
			gotoAndPlay(MENU);
		}
		//END Button Management
		
		//START Input Management
		protected function onKDown(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case LEFT: 
				_leftPressed = true;
				break;
				case RIGHT: 
				_rightPressed = true;
				break;
				case DOWN: 
				_downPressed = true;
				break;
				case UP: 
				_upPressed = true;
				break;
		}
		}
		
		protected function onKUp(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case LEFT: 
				character.gotoAndPlay("start");
				_leftPressed = false;
				break;
				case RIGHT: 
				character.gotoAndPlay("start");
				_rightPressed = false;
				break;
				case DOWN: 
				_downPressed = false;
				break;
				case UP: 
				_upPressed = false;
				break;
			}
		}
		
		
		//END Input Management
		//character.x -= power;	character.gotoAndPlay("left");
		//character.x += power;	character.gotoAndPlay("right");
		
		//MAIN GAME LOOP
		protected function update(e:Event)
		{
			handleKeyInput();
			checkCharacterCollision();
			movePlayer();
		}
		
		protected function checkCharacterCollision()
		{
			// Bottom Left - check clip 
			if(mazeClip.hitTestPoint(_playerNextX+11,_playerNextY+28,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+11,_playerNextY+28,true))
				{
					_playerNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_playerNextX+11,character.y+28,true))
				{
					_playerNextY = character.y;
				}
				
			}
			// Bottom Right
			if(mazeClip.hitTestPoint(_playerNextX+24,_playerNextY+28,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+24,_playerNextY+28,true))
				{
					_playerNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_playerNextX+24,character.y+28,true))
				{
					_playerNextY = character.y;
				}

			}
			// Top Left
			if(mazeClip.hitTestPoint(_playerNextX+6,_playerNextY+4,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+6,_playerNextY+4,true))
				{
					_playerNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_playerNextX+6,character.y+4,true))
				{
					_playerNextY = character.y;
				}
			}
			// Top Right
			if(mazeClip.hitTestPoint(_playerNextX+24,_playerNextY+4,true))
			{
				// if the next move position would hit 
				// would it if there was no x movement 
				if(mazeClip.hitTestPoint(character.x+24,_playerNextY+4,true))
				{
					_playerNextX = character.x;
				}
				// if the next move position would hit 
				// would it if there was no y movement
				if(mazeClip.hitTestPoint(_playerNextX+24,character.y+4,true))
				{
					_playerNextY = character.y;
				}
			}
			
			// check the fires
			// Bottom Left
			if(firesClip.hitTestPoint(_playerNextX+11,_playerNextY+28,true))
			{
				trace("hit fire");
				_playerNextX = 0;
				_playerNextY = 32;
			}
			// Bottom Right
			if(firesClip.hitTestPoint(_playerNextX+24,_playerNextY+28,true))
			{
				trace("hit fire");
				_playerNextX = 0;
				_playerNextY = 32;
			}
			// Top Left
			if(firesClip.hitTestPoint(_playerNextX+6,_playerNextY+4,true))
			{
				trace("hit fire");
				_playerNextX = 0;
				_playerNextY = 32;
			}
			// Top Right
			if(firesClip.hitTestPoint(_playerNextX+24,_playerNextY+4,true))
			{
				trace("hit fire");
				_playerNextX = 0;
				_playerNextY = 32;
			}
			
			// check exit
			// Bottom Left
			if(exitClip.hitTestPoint(_playerNextX+11,_playerNextY+28,true))
			{
				exitGame();
			}
			//Bottom Right
			if(exitClip.hitTestPoint(_playerNextX+24,_playerNextY+28,true))
			{
				exitGame();
			}
			// Top Left
			if(exitClip.hitTestPoint(_playerNextX+6,_playerNextY+4,true))
			{
				exitGame();
			}
			// Top Right
			if(exitClip.hitTestPoint(_playerNextX+24,_playerNextY+4,true))
			{
				exitGame();
			}

		}
		
		protected function handleKeyInput()
		{
			// set the _next position to the current position
			_playerNextX = character.x;
			_playerNextY = character.y;

			if(_leftPressed)
			{
				_playerNextX = character.x - _power;
				trace(character.currentLabel);
				character.nextFrame();
				if(character.currentLabel != "left")
				{
					character.gotoAndStop("left");
				}

			}
			if(_rightPressed)
			{
				_playerNextX = character.x + _power;
				
				character.nextFrame();
				trace(character.currentLabel);
				if(character.currentLabel != "right")
				{
					character.gotoAndStop("right");
				}
			}
			if(_upPressed)
			{
				_playerNextY = character.y - _power;
			}
			if(_downPressed)
			{
				_playerNextY = character.y + _power;
			}
		}
		
		protected function movePlayer()
		{
			if(_playerNextX>=0 && _playerNextX<=500)
			{
				character.x = _playerNextX;
			}
			if(_playerNextY>=0 && _playerNextY<=350)
			{
				character.y = _playerNextY;
			}
		}
	}
}